<?php

/**
 * tiket actions.
 *
 * @package    sf_sandbox
 * @subpackage tiket
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class tiketActions extends sfActions
{

    public function executeShow(sfWebRequest $request)
  {
      $this->m = MailboxPeer::retrieveByPK($request->getParameter('id'));
      $this->m = 
      $this->m->setFlRead(true);
      $this->m->save();
      $this->setLayout('video_layout');
  }
  
   public function executeDel(sfWebRequest $request)
  {
      $m =  MailboxPeer::retrieveByPK($request->getParameter('id'));
      if($this->getUser()->isSuperAdmin()) $m->delete();
      $this->redirect('tiket/index');
  }


  public function executeIndex(sfWebRequest $request)
  {
    
      $c = new Criteria();



            if($this->getUser()->hasCredential('partner') and !$this->getUser()->isSuperadmin() )
            {
                $crit1=$c->getNewCriterion(MailboxPeer::FROM_ID, $this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
                $crit2=$c->getNewCriterion(MailboxPeer::TO_ID,$this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
                $crit1->addOr($crit2);
                $c->add($crit1);
            }
            else
            {
                $crit1=$c->getNewCriterion(MailboxPeer::FROM_ID, 2);
                $crit2=$c->getNewCriterion(MailboxPeer::TO_ID,2);
                $crit1->addOr($crit2);
                $c->add($crit1);
            }


               if($request->getParameter('no_reply')=='true')
                {
                   $c->add(MailboxPeer::FL_READ, false);
                }

          if($request->getParameter('partner'))
              {
                $crit1=$c->getNewCriterion(MailboxPeer::FROM_ID, $request->getParameter('partner'));
                $crit2=$c->getNewCriterion(MailboxPeer::TO_ID,$request->getParameter('partner'));
                $crit1->addOr($crit2);
                $c->add($crit1);
              }

        if(!$request->getParameter('page')){ $this->page = 1;}
        else { $this->page=$request->getParameter('page');}
        $c->addDescendingOrderByColumn(MailboxPeer::ID);
        $pager = new sfPropelPager ( 'Mailbox', 10 );
        $pager->setCriteria ( $c );
        $pager->setPeerMethod ( 'doSelect' );
        $pager->setPage ( $this->page );
        $pager->init ();
        $this->mailbox_list  = $pager;

  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new tiketForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new tiketForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($mailbox = MailboxPeer::retrieveByPk($request->getParameter('id')), sprintf('Object mailbox does not exist (%s).', $request->getParameter('id')));
    $this->form = new tiketForm($mailbox);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($mailbox = MailboxPeer::retrieveByPk($request->getParameter('id')), sprintf('Object mailbox does not exist (%s).', $request->getParameter('id')));
    $this->form = new tiketForm($mailbox);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($mailbox = MailboxPeer::retrieveByPk($request->getParameter('id')), sprintf('Object mailbox does not exist (%s).', $request->getParameter('id')));
    $mailbox->delete();

    $this->redirect('tiket/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $mailbox = $form->save();
      if($request->getParameter('reply'))
          {
              $form->getObject()->setFlReply(true);
              $form->getObject()->save();
          }

      $this->redirect('tiket/index');
    }
  }
}
